﻿/*
 * @author: wizardc
 */

using Unity.Mathematics;

namespace MessagePack.Formatters
{
    public class Float2Formatter : IMessagePackFormatter<float2>
    {
        public static readonly Float2Formatter Instance = new Float2Formatter();

        private Float2Formatter()
        {
        }

        public void Serialize(ref MessagePackWriter writer, float2 value, MessagePackSerializerOptions options)
        {
            writer.WriteArrayHeader(2);
            writer.Write(value.x);
            writer.Write(value.y);
        }

        public float2 Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            reader.ReadArrayHeader();
            var x = reader.ReadSingle();
            var y = reader.ReadSingle();
            return new float2(x, y);
        }
    }
    
    public class Float4x4Formatter : IMessagePackFormatter<float4x4>
    {
        public static readonly Float4x4Formatter Instance = new Float4x4Formatter();

        private Float4x4Formatter()
        {
        }

        public void Serialize(ref MessagePackWriter writer, float4x4 value, MessagePackSerializerOptions options)
        {
            writer.WriteArrayHeader(16);
            for (int i = 0; i < 4; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    writer.Write(value[i][j]);
                }
            }
        }

        public float4x4 Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            reader.ReadArrayHeader();
            var matrix = new float4x4();
            for (int i = 0; i < 4; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    matrix[i][j] = reader.ReadSingle();
                }
            }
            return matrix;
        }
    }
}
